﻿using AiBLSmartEdu.Module.AIMedicalAssistant.API.Configurations;
using AiBLSmartEdu.Module.AIMedicalAssistant.API.Hubs;
using FrameworkCore.Modules.Startups;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace AiBLSmartEdu.Module.AIMedicalAssistant.API
{
    public class AiBLSmartEduStartup : IAiBLSmartEduStartup
    {
        public int Order => 3000;

        public void Configure(IApplicationBuilder application)
        {
            // 启用身份验证
            application.UseAuthentication();
            application.UseAuthorization();

            // 执行由路由中间件选择的端点
            application.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<NotificationHub>("/aimedicalassistant/notification");
            });
        }

        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            // 注册配置文件
            var config = ConfigurationsExtensions.AddJsonFile("aimedicalssistant");
            services.Configure<ChromaSettings>(config.GetSection(ChromaSettings.Key));
            services.Configure<KnowledgeBaseSettings>(config.GetSection(KnowledgeBaseSettings.Key));
            services.Configure<OllamaSettings>(config.GetSection(OllamaSettings.Key));
            services.AddHttpClient("Ollama", client =>
            {
                OllamaSettings settings = config.GetSection(OllamaSettings.Key).Get<OllamaSettings>()!;
                client.BaseAddress = new Uri(settings.BaseUrl);
            });

            services.AddSignalR();
        }
    }
}
